package com.hspedu.mhl.view;

import com.hspedu.mhl.domain.*;
import com.hspedu.mhl.service.AthleteService;
import com.hspedu.mhl.service.BillService;
import com.hspedu.mhl.service.CompanionsService;
import com.hspedu.mhl.service.CourtTableService;
import com.hspedu.mhl.utils.Utility;

import javax.swing.text.View;
import java.util.List;

public class MHLview {
    private boolean loop = true;
    private String key = " ";
    private AthleteService athleteService = new AthleteService();
    CourtTableService courtTableService = new CourtTableService();
    CompanionsService companionsService = new CompanionsService();
    BillService billService = new BillService();

    public static void main(String[] args) {
        new MHLview().mainMenu();
    }

    //遍历场地信息并打印出来
    public void courtTable() {
        System.out.println("View reservation status");
        List<Court> courtTable = courtTableService.getCourtTable();
        for (Court court : courtTable) {
            System.out.println(court);
        }
    }

    //检查用户预定场地的方法（是否存在+更新状态）
    public void updateCourtTable() {
        System.out.println("===========Update reservation status===========");
        System.out.println("Enter the courtid you want to book");
        int court_id = Utility.readInt();
        if (court_id == -1) {
            System.out.println("Invalid courtid");
            return;
        }
        char key = Utility.readConfirmSelection();
        if (key == 'Y') {
            Court courtTableById = courtTableService.getCourtTableById(court_id);
            if (courtTableById == null) {
                System.out.println("Login failed");
                return;
            }
            if (!("空".equals(courtTableById.getState()))) {
                System.out.println("The court is already reserved");
                return;
            }

            System.out.println("Enter the username to be reserved");
            String ordername = Utility.readString(50);
            System.out.println("Enter the phone number to be reserved");
            String orderTel = Utility.readString(50);

            courtTableService.updateCourt(court_id, ordername, orderTel);
        } else {
            System.out.println("Invalid courtid");
        }
    }

    //给用户显示同伴菜单
    public void courtTableSearch() {
        System.out.println("Enter the courtid you want to search");
        List<Companions> companions = companionsService.getCompanions();
        for (Companions companion : companions) {
            System.out.println(companion);
        }
    }

    //完成点单服务
    public void book() {
        System.out.println("Enter the companion_id you want to book");
        int companion_id = Utility.readInt();
        System.out.println("Enter the court_id you are");
        int court_id = Utility.readInt();
        System.out.println("Enter the nums you want to book");
        int nums = Utility.readInt();
        billService.orderBill(companion_id, nums, court_id);
    }

    //查看点单后的账单信息
    public void bookSearch() {
        System.out.println("The information of the BookList");
        List<MultiTableBean> billList1 = billService.getBillList1();
        for (MultiTableBean bill : billList1) {
            System.out.println(bill);
        }
    }

    public void mainMenu() {
        //显示主菜单
        while (loop) {
            System.out.println("===========Welcome to Australian2 Open!!!============");
            System.out.println("\t\t 1 Log in to the website");
            System.out.println("\t\t 2 LOGOUT");
            System.out.println("Please enter your selection: ");
            key = Utility.readString(1);

            switch (key) {
                case "1":
                    System.out.println("Please enter the athlete's World Ranking:");
                    String athId = Utility.readString(20);
                    System.out.println("Please enter the athlete password:");
                    String password = Utility.readString(20);
                    Athlete athleteByIdAndPwd = athleteService.getAthleteByIdAndPwd(athId, password);

                    //到数据库中判断
                    if (athleteByIdAndPwd != null) {
                        System.out.println("[" + athleteByIdAndPwd.getNAME() + "]" +
                                "You are logged in to the website");
                        //登陆成功，进入二级菜单

                        while (loop) {
                            System.out.println("Welcome to the Australian Open Service Center~~");
                            System.out.println("Please enter your next selection: ");
                            System.out.println("\t\t 1 View reservation status");
                            System.out.println("\t\t 2 Booking a venue");
                            System.out.println("\t\t 3 Display all equipment and training companions");
                            System.out.println("\t\t 4 Booking service");
                            System.out.println("\t\t 5 View bill");
                            System.out.println("\t\t 6 payment");
                            System.out.println("\t\t 9 Log out");
                            System.out.println("Please enter your selection: ");

                            key = Utility.readString(1);
                            switch (key) {
                                case "1":
                                    courtTable();
                                    break;
                                case "2":
                                    updateCourtTable();
                                    break;
                                case "3":
                                    courtTableSearch();
                                    break;
                                case "4":
                                    book();
                                    break;
                                case "5":
                                    bookSearch();
                                    break;
                                case "6":
                                    System.out.println("payment");
                                    break;
                                case "9":
                                    System.out.println("Log out");
                                    loop = false;
                                    break;
                                default:
                                    break;
                            }
                        }
                    } else {
                        System.out.println("You are not logged in to the website");
                    }
                    break;
                case "2":
                    System.out.println("\t\t Log out");
                    loop = false;
                    break;
                default:
                    System.out.println("Invalid selection");

            }

        }
    }
}
